<?php
/**
 * Opencart BeanStream Payment Module - Admin
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * @category   Opencart
 * @package    Payment
 * @copyright  Copyright (c) 2010 Schogini Systems (http://www.schogini.in)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Gayatri S Ajith <gayatri@schogini.com>
 */
class ControllerPaymentBeanstream extends Controller {
	private $error = array(); 

	public function index() {
		$this->load->language('payment/beanstream');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('setting/setting');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('beanstream', $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');
			
			$this->redirect($this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_all_zones'] = $this->language->get('text_all_zones');
		$this->data['text_authorization'] = $this->language->get('text_authorization');
		$this->data['text_capture'] = $this->language->get('text_capture');		
		
		$this->data['entry_merchant_id'] = $this->language->get('entry_merchant_id');
		$this->data['entry_merchant_id_cad'] = $this->language->get('entry_merchant_id_cad');
		$this->data['entry_username'] = $this->language->get('entry_username');
		$this->data['entry_password'] = $this->language->get('entry_password');
		
		$this->data['entry_username_cad'] = $this->language->get('entry_username_cad');
		$this->data['entry_password_cad'] = $this->language->get('entry_password_cad');
		
		$this->data['entry_key'] = $this->language->get('entry_key');
		$this->data['entry_mode'] = $this->language->get('entry_mode');
		$this->data['entry_method'] = $this->language->get('entry_method');
		$this->data['entry_order_status'] = $this->language->get('entry_order_status');		
		$this->data['entry_geo_zone'] = $this->language->get('entry_geo_zone');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['merchant_id'])) {
			$this->data['error_merchant_id'] = $this->error['merchant_id'];
		} else {
			$this->data['error_merchant_id'] = '';
		}
		
		if (isset($this->error['merchant_id_cad'])) {
			$this->data['error_merchant_id_cad'] = $this->error['merchant_id_cad'];
		} else {
			$this->data['error_merchant_id_cad'] = '';
		}
		
 		if (isset($this->error['username'])) {
			$this->data['error_username'] = $this->error['username'];
		} else {
			$this->data['error_username'] = '';
		}
 		if (isset($this->error['password'])) {
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}	

		if (isset($this->error['username_cad'])) {
			$this->data['error_username_cad'] = $this->error['username_cad'];
		} else {
			$this->data['error_username_cad'] = '';
		}
 		if (isset($this->error['password_cad'])) {
			$this->data['error_password_cad'] = $this->error['password_cad'];
		} else {
			$this->data['error_password_cad'] = '';
		}			

 		if (isset($this->error['key'])) {
			$this->data['error_key'] = $this->error['key'];
		} else {
			$this->data['error_key'] = '';
		}

 		if (isset($this->error['hash'])) {
			$this->data['error_hash'] = $this->error['hash'];
		} else {
			$this->data['error_hash'] = '';
		}
		
  		$this->document->breadcrumbs = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_payment'),
			'href'      => $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('payment/beanstream', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		
		$this->data['action'] = $this->url->link('payment/beanstream', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['cancel'] = $this->url->link('extension/payment', 'token=' . $this->session->data['token'], 'SSL');	
		
		if (isset($this->request->post['beanstream_merchant_id'])) {
			$this->data['beanstream_merchant_id'] = $this->request->post['beanstream_merchant_id'];
		} else {
			$this->data['beanstream_merchant_id'] = $this->config->get('beanstream_merchant_id');
		}	
		
		if (isset($this->request->post['beanstream_merchant_id_cad'])) {
			$this->data['beanstream_merchant_id_cad'] = $this->request->post['beanstream_merchant_id_cad'];
		} else {
			$this->data['beanstream_merchant_id_cad'] = $this->config->get('beanstream_merchant_id_cad');
		}	


		
		if (isset($this->request->post['beanstream_username'])) {
			$this->data['beanstream_username'] = $this->request->post['beanstream_username'];
		} else {
			$this->data['beanstream_username'] = $this->config->get('beanstream_username');
		}
		if (isset($this->request->post['beanstream_password'])) {
			$this->data['beanstream_password'] = $this->request->post['beanstream_password'];
		} else {
			$this->data['beanstream_password'] = $this->config->get('beanstream_password');
		}
		
		if (isset($this->request->post['beanstream_username_cad'])) {
			$this->data['beanstream_username_cad'] = $this->request->post['beanstream_username_cad'];
		} else {
			$this->data['beanstream_username_cad'] = $this->config->get('beanstream_username_cad');
		}
		if (isset($this->request->post['beanstream_password_cad'])) {
			$this->data['beanstream_password_cad'] = $this->request->post['beanstream_password_cad'];
		} else {
			$this->data['beanstream_password_cad'] = $this->config->get('beanstream_password_cad');
		}
	
		if (isset($this->request->post['beanstream_key'])) {
			$this->data['beanstream_key'] = $this->request->post['beanstream_key'];
		} else {
			$this->data['beanstream_key'] = $this->config->get('beanstream_key');
		}
		
		if (isset($this->request->post['beanstream_mode'])) {
			$this->data['beanstream_mode'] = $this->request->post['beanstream_mode'];
		} else {
			$this->data['beanstream_mode'] = $this->config->get('beanstream_mode');
		}
		
		if (isset($this->request->post['beanstream_method'])) {
			$this->data['beanstream_method'] = $this->request->post['beanstream_method'];
		} else {
			$this->data['beanstream_method'] = $this->config->get('beanstream_method');
		}
		
		if (isset($this->request->post['beanstream_order_status_id'])) {
			$this->data['beanstream_order_status_id'] = $this->request->post['beanstream_order_status_id'];
		} else {
			$this->data['beanstream_order_status_id'] = $this->config->get('beanstream_order_status_id'); 
		} 

		$this->load->model('localisation/order_status');
		
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
		
		if (isset($this->request->post['beanstream_geo_zone_id'])) {
			$this->data['beanstream_geo_zone_id'] = $this->request->post['beanstream_geo_zone_id'];
		} else {
			$this->data['beanstream_geo_zone_id'] = $this->config->get('beanstream_geo_zone_id'); 
		} 
		
		$this->load->model('localisation/geo_zone');
										
		$this->data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
		
		if (isset($this->request->post['beanstream_status'])) {
			$this->data['beanstream_status'] = $this->request->post['beanstream_status'];
		} else {
			$this->data['beanstream_status'] = $this->config->get('beanstream_status');
		}
		
		if (isset($this->request->post['beanstream_sort_order'])) {
			$this->data['beanstream_sort_order'] = $this->request->post['beanstream_sort_order'];
		} else {
			$this->data['beanstream_sort_order'] = $this->config->get('beanstream_sort_order');
		}

		$this->template = 'payment/beanstream.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

	private function validate() {
		if (!$this->user->hasPermission('modify', 'payment/beanstream')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}
		
		//if (!$this->request->post['beanstream_merchant_id']) {
		if (!$this->request->post['beanstream_merchant_id'] && !$this->request->post['beanstream_merchant_id_cad']) {
			$this->error['merchant_id'] = $this->language->get('error_merchant_id');
		}
		
		/*if (!$this->request->post['beanstream_merchant_id_cad']) {
			$this->error['merchant_id_cad'] = $this->language->get('error_merchant_id_cad');
		}*/
		
		// if username is filled and password is empty - its an error!
		if ($this->request->post['beanstream_username'] && !$this->request->post['beanstream_password']) {
			$this->error['password'] = $this->language->get('error_password');
		}		
		
		if ($this->request->post['beanstream_username_cad'] && !$this->request->post['beanstream_password_cad']) {
			$this->error['password_cad'] = $this->language->get('error_password_cad');
		}		
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}	
	}
}
?>